package uk.co.hs.web.security.filter;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

import uk.co.hs.service.admin.interfaces.WebUserServiceInterface;

/**
 * AdminAuthenticationSuccessHandler.
 */
public class AdminAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler
{
  @Autowired(required=true)
  @Qualifier("hs_webUserService")
  private WebUserServiceInterface mUserService;

	/**
	 * @see org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler
	 * #onAuthenticationSuccess(javax.servlet.http.HttpServletRequest,
	 *                          javax.servlet.http.HttpServletResponse,
	 *                          org.springframework.security.core.Authentication)
	 * {@inheritDoc}
	 */
  @Override
	public void onAuthenticationSuccess(HttpServletRequest aRequest,
			                                HttpServletResponse aResponse,
			                                Authentication aAuth) throws IOException, ServletException
	{
		String userName = aAuth.getName();
		mUserService.resetFailedLoginAttempts(userName);
		super.onAuthenticationSuccess(aRequest, aResponse, aAuth);
	}
}
